package com.bonait.hotapp;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PathUtils;
import com.bonait.hotapp.utils.UpdateUtil;
import com.tencent.tinker.lib.tinker.TinkerInstaller;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button loadPatch;
    private Button loadPatch1;
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        verifyStoragePermissions(this);
        initView();
        initListener();
    }

    private void initListener() {
        loadPatch.setOnClickListener(this);
        loadPatch1.setOnClickListener(this);
    }

    private void initView() {
        loadPatch = findViewById(R.id.btn_loadpatch);
        loadPatch1 = findViewById(R.id.btn_loadpatch1);
    }

    public static void verifyStoragePermissions(Activity activity) {
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载补丁
     */
    private void loadPatch(String path) {
        File patchFile = new File(path);
        if (!patchFile.exists()) {
            Toast.makeText(this, "load error", Toast.LENGTH_SHORT).show();
            return;
        } else {
            Toast.makeText(this, "start load", Toast.LENGTH_SHORT).show();
        }

        LogUtils.d("itchen---->" + patchFile.getAbsolutePath());
        //itchen---->/storage/emulated/0/Android/data/com.bonait.hotapp/files/patch/patch_signed_7zip.apk
        TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(), patchFile.getAbsolutePath());
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();

        if (id == R.id.btn_loadpatch) {

            LogUtils.d("yue-tag", "666");

            //loadPatch(PathUtils.getExternalAppFilesPath() + "/" + "patch" + "/" + "patch_signed_7zip.apk");


            UpdateUtil.newInstance(new UpdateUtil.UpdateListener() {
                @Override

                public void DownloadSuccess(String appUrl) {

                    LogUtils.d("itchen---yue-tag", "下载成功本地保存路径为：" + appUrl);

                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            loadPatch(appUrl);
                        }
                    });
                }

                @Override
                public void DownloadFailure(String err) {
                    LogUtils.d("itchen---yue-tag", "下载失败" + err);
                }

            }).startDownload();

        } else if (id == R.id.btn_loadpatch1) {

            Toast.makeText(this, "itchen--start load---chenqi 第一次加载测试", Toast.LENGTH_LONG).show();

            LogUtils.d("itchen---->" + PathUtils.getExternalAppFilesPath() + "/" + "patch" + "/" + "patch_signed_7zip.apk");

            //itchen---->/storage/emulated/0/Android/data/com.bonait.hotapp/files/patch/patch_signed_7zip.apk
            //    itchen---->/storage/emulated/0/Android/data/com.bonait.hotapp/files/patch/patch_signed_7zip.apk

        }


    }
}